var express = require('express');
var superagent = require('superagent');
var cheerio = require('cheerio');
var url = require('url');
var router = express.Router();

/* GET home page. */
var cnodeUrl = 'https://cnodejs.org/';
router.get('/', function (req, res, next) {

    superagent.get(cnodeUrl)
        .end(function (err, sres) {

            if (err) {
                return next(err);
            }

            var $ = cheerio.load(sres.text);
            var items = [];
            $('#topic_list .topic_title').each(
                function (idx, element) {
                    var $element = $(element);
                    items.push({
                        title: $element.attr('title'),
                        href: $element.attr('href'),
                        link: url.resolve(cnodeUrl, $element.attr('href'))
                    })
                }
            )
            res.render('index-nav',{items:items});
        });
});

router.get('/nav',function(req,res,next){
    res.render('index-nav',{title:'title'})
});
module.exports = router;
